//
//  CustomCalloutView.m
//  testMap
//
//  Created by 林振宇 on 15/4/22.
//  Copyright (c) 2015年 www.lanou3g.com. All rights reserved.
//

#define kPortraitMargin 5 
#define kPortraitWidth 70 
#define kPortraitHeight 50
#define kTitleWidth         120
#define kTitleHeight        20

#import "CustomCalloutView.h"
//绘图使用的宏
#define kArrorHeight        10

@interface CustomCalloutView ()
@property (nonatomic, retain) UIImageView *portraitView;
@property (nonatomic, retain) UILabel *subtitleLabel;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UIButton *detailsButton;
@end
@implementation CustomCalloutView
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews
{
    // 添加图片
    self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(kPortraitMargin, kPortraitMargin, kPortraitWidth, kPortraitHeight)];
    
    self.portraitView.backgroundColor = [UIColor blackColor];
    [self addSubview:self.portraitView];
    
    // 添加标题
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 + kPortraitWidth, kPortraitMargin, kTitleWidth, kTitleHeight)];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.text = @"titletitletitletitle";
    [self addSubview:self.titleLabel];
    
    self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 + kPortraitWidth, kPortraitMargin * 2 + kTitleHeight, kTitleWidth, kTitleHeight)];
    self.subtitleLabel.font = [UIFont systemFontOfSize:12];
    self.subtitleLabel.textColor = [UIColor lightGrayColor];
    self.subtitleLabel.text = @"subtitleLabelsubtitleLabelsubtitleLabel";
    [self addSubview:self.subtitleLabel];
    
    self.detailsButton = [UIButton buttonWithType:UIButtonTypeCustom] ;
    self.detailsButton.frame =CGRectMake(self.titleLabel.frame.origin.x+self.titleLabel.frame.size.width+5, self.titleLabel.frame.origin.y+3, self.frame.size.width-self.titleLabel.frame.origin.x-self.titleLabel.frame.size.width-8,self.titleLabel.frame.size.height+self.subtitleLabel.frame.size.height);
    [self.detailsButton.layer setCornerRadius:10];
    self.detailsButton.backgroundColor = [UIColor lightGrayColor];
    //self.detailsButton.alpha = 0.3;
    //self.detailsButton.titleLabel.text = @"1";
    self.detailsButton.titleLabel.numberOfLines = 0;
    self.detailsButton.titleLabel.textColor = [UIColor whiteColor];
    self.detailsButton.titleLabel.alpha=0.9;
    self.detailsButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.detailsButton addTarget:self action:@selector(detailsAction) forControlEvents:UIControlEventTouchUpInside];
    [self.detailsButton setTitle:@"显示\n详情" forState:UIControlStateNormal];
    
    [self addSubview:self.detailsButton];
    
    
}
-(void)detailsAction
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:self.details delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
    [alertView show];
    //[alertView release];
}
-(void)layoutSubviews
{
     self.titleLabel.text = self.title;
    self.subtitleLabel.text = self.subtitle;
    self.portraitView.image = self.image;
           NSLog(@"aaaaa%@",self.subtitle);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


#pragma  mark -自定义气泡背景 灰色背景带下面的小三角 使用的是绘图类.绘上去的

- (void)drawRect:(CGRect)rect
{
     
    [self drawInContext:UIGraphicsGetCurrentContext()];
    self.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}
- (void)drawInContext:(CGContextRef)context
{
    //设置线条宽度
    CGContextSetLineWidth(context, 2.0);
    //设直颜色
    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.8].CGColor);
    
    [self getDrawPath:context];
    //填充称实心和颜色
    CGContextFillPath(context);
}
//绘制路径
- (void)getDrawPath:(CGContextRef)context
{

    CGRect rrect = self.bounds;
    CGFloat radius = 6.0;
    CGFloat minx = CGRectGetMinX(rrect),
    midx = CGRectGetMidX(rrect),
    maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect),
    maxy = CGRectGetMaxY(rrect)-kArrorHeight;
    
    
    CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
    CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
    CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextClosePath(context);
}
@end
